package com.hx.mypetstore;

import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.FrameLayout;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.hx.mypetstore.ui.main.mainFragment;
import com.hx.mypetstore.ui.cart.CartFragment;
import com.hx.mypetstore.ui.my.myCentFragment;
import com.hx.mypetstore.util.LoginUtil;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    //UI Object
    private TextView txt_topbar;
    private TextView txt_main;
    private TextView txt_cart;
    private TextView txt_my;
    private FrameLayout ly_content;

    //Fragment Object
    private mainFragment fg1;
    private CartFragment fg2;
    private myCentFragment fg3;
    private FragmentManager fManager;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);
        fManager = getSupportFragmentManager();
        bindViews();
        txt_main.performClick();   //模拟一次点击，既进去后选择第一项
    }
    //UI组件初始化与事件绑定
    private void bindViews() {
        txt_topbar =  findViewById(R.id.txt_topbar);
        txt_main = findViewById(R.id.txt_main);
        txt_cart = findViewById(R.id.txt_cart);
        txt_my = findViewById(R.id.txt_my);
        ly_content = findViewById(R.id.ly_content);

        txt_main.setOnClickListener(this);
        txt_cart.setOnClickListener(this);
        txt_my.setOnClickListener(this);
    }

    //重置所有文本的选中状态
    private void setSelected(){
        txt_main.setSelected(false);
        txt_cart.setSelected(false);
        txt_my.setSelected(false);
    }

    //隐藏所有Fragment
    private void hideAllFragment(androidx.fragment.app.FragmentTransaction fragmentTransaction){
        if(fg1 != null)fragmentTransaction.hide(fg1);
        if(fg2 != null)fragmentTransaction.hide(fg2);
        if(fg3 != null)fragmentTransaction.hide(fg3);
    }


    @Override
    public void onClick(View v) {
        FragmentTransaction fTransaction = fManager.beginTransaction();
        hideAllFragment(fTransaction);
        switch (v.getId()){
            case R.id.txt_main:
                setSelected();
                txt_main.setSelected(true);
                if(fg1 == null){
                    fg1 = new mainFragment();
                    fTransaction.add(R.id.ly_content,fg1);
                }else{
                    fTransaction.show(fg1);
                }
                break;
            case R.id.txt_cart:
                setSelected();
                txt_cart.setSelected(true);
                if(fg2 == null){
                    fg2 = new CartFragment();
                    fTransaction.add(R.id.ly_content,fg2);
                }else{
                    fTransaction.show(fg2);
                }
                break;
            case R.id.txt_my:
                setSelected();
                txt_my.setSelected(true);
                if(!LoginUtil.loginCheck(this))
                    ;
                else {
                    if (fg3 == null) {
                        fg3 = new myCentFragment();
                        fTransaction.add(R.id.ly_content, fg3);
                    } else {
                        fTransaction.show(fg3);
                    }
                }
                break;
        }
        fTransaction.commit();
    }
}